#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <fstream>

//通过json库来实现自动的序列化和反序列化
//这里除了要要指定头文件之外，还要指定具体的实现，即库
//路径时默认的路径，但是要指定库的名称
int main()
{
	Json::Value root; //万能对象——起手式
	root["name"] = "Joey";//设置键值对
	root["sex"] = "Male";
	root["age"] = "18";

	Json::Value sub;
	sub["tel"] = "12345";
	sub["address"] = "Shandong";

	root["info"] = sub;

	//toStyledString以类似于结构体的多行方式输出长字符串
	//std::string s = root.toStyledString();
	Json::StyledWriter writer1;
	std::string s = writer1.write(root);

	//fastwriter以按行输出的方式输出长字符串
	Json::FastWriter writer;
	std::string s1 = writer.write(root);
	std::cout << s1 << std::endl;

	std::cout << s << std::endl;

	//-----------------------------------------------------------

	std::stringstream ss;
	Json::StreamWriterBuilder sbuilder;
	std::unique_ptr<Json::StreamWriter> writer2(sbuilder.newStreamWriter());
	writer2->write(root, &ss);
	std::string s2 = ss.str();
	std::cout << s << std::endl;

	return 0;
}
